<?php

namespace addons\cms\model;

use app\admin\model\User;
use app\common\library\Auth;
use app\common\library\Email;
use think\Exception;
use think\Model;
use think\Validate;

/**
 * 评论模型
 */
class User_follow Extends Model
{

    protected $name = "cms_user_follow";
    //自定义初始化
    protected static function init()
    {

    }


    /**
     * 发表评论
     * @param array $params
     * @return bool
     * @throws Exception
     */
    public static function postFollow($follow_id)
    {
        $request = request();
        $request->filter('strip_tags');
        $useragent = $request->server('HTTP_USER_AGENT', '');
        $ip = $request->ip();
        $auth = Auth::instance();

        if (!$auth->id) {
            return [
                'code'=>-1,
                'msg'=>"请登录后进行关注"
            ];
        }
        //查找是否关注
        $is_collection = self::where(['follow_id' =>$follow_id, 'user_id' =>  $auth->id])->find();
        if ($is_collection) {
            return [
                'code'=>-1,
                'msg'=>"你已经关注过了,请勿重复操作"
            ];
        }

        $params['user_id'] = $auth->id;
        $params['follow_id'] = $follow_id;
        (new static())->allowField(true)->save($params);

        return true;
    }

    /**
     * 取消关注
     * @param array $params
     * @return bool
     * @throws Exception
     */
    public static function quFollow($follow_id)
    {
        $request = request();
        $request->filter('strip_tags');
        $useragent = $request->server('HTTP_USER_AGENT', '');
        $ip = $request->ip();
        $auth = Auth::instance();

        if (!$auth->id) {
            return [
                'code'=>-1,
                'msg'=>"请登录后进行操作"
            ];
        }

        self::where(['follow_id' =>$follow_id, 'user_id' =>  $auth->id])->delete();

        return true;
    }

    /**
     * 获取我的关注
     * @param array $params
     * @return bool
     * @throws Exception
     */
    public static function get_guanzhu()
    {
        $request = request();
        $request->filter('strip_tags');
        $useragent = $request->server('HTTP_USER_AGENT', '');
        $ip = $request->ip();
        $auth = Auth::instance();

        if (!$auth->id) {
            return [
                'code'=>-1,
                'msg'=>"请登录后进行操作"
            ];
        }
        $follow_ids = collection(self::where([ 'user_id' =>  $auth->id])->field('follow_id')->select())->toArray();
        $ids = '';
        foreach ($follow_ids as $k=>$v){
            $ids .= ','.$v['follow_id'];
        }
        $ids = trim($ids,',');

        return User::where('id','in',$ids)->select();
    }

    /**
     * 获取我的粉丝
     * @param array $params
     * @return bool
     * @throws Exception
     */
    public static function get_fen()
    {
        $request = request();
        $request->filter('strip_tags');
        $useragent = $request->server('HTTP_USER_AGENT', '');
        $ip = $request->ip();
        $auth = Auth::instance();

        if (!$auth->id) {
            return [
                'code'=>-1,
                'msg'=>"请登录后进行操作"
            ];
        }
        $follow_ids = collection(self::where([ 'follow_id' =>  $auth->id])->field('user_id')->select())->toArray();
        $ids = '';
        foreach ($follow_ids as $k=>$v){
            $ids .= ','.$v['user_id'];
        }
        $ids = trim($ids,',');
        $fen = User::where('id','in',$ids)->select();

        $guan_ids = collection(self::where([ 'user_id' =>  $auth->id])->select())->toArray();

        $gids = '';
        foreach ($guan_ids as $k=>$v){
            $gids .= ','.$v['follow_id'];
        }
        $gids = trim($gids,',');

        $ids_arr = explode(',',$gids);
        foreach ($fen as $k=>$v){
            if(in_array($v['id'],$ids_arr)){
                $fen[$k]['follow'] = '取消关注';
            }else{
                $fen[$k]['follow'] = '关注';
            }
        }
        return $fen;
    }

    /**
     * 关联会员模型
     */
    public function user()
    {
        return $this->belongsTo("app\common\model\User")->field('id,nickname,avatar,email')->setEagerlyType(1);
    }

    /**
     * 关联文章模型
     */
    public function archives()
    {
        return $this->belongsTo("addons\cms\model\Archives")->field('id,title,image,diyname,description,collection,model_id,money,channel_id,likes,dislikes,tags,createtime')->setEagerlyType(1);
    }

}
